package cn.itcast.ft.service.impl;

import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;
import cn.itcast.common.PageGoodsCommonDTO;
import cn.itcast.ft.dto.FtGoodsDTO;
import cn.itcast.ft.dto.PageGoodsdDetailDTO;
import cn.itcast.ft.service.FTService;
import cn.itcast.utils.DingDingUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.net.Proxy;
import java.util.ArrayList;
import java.util.List;

/**
 * Author:zhansan
 * Date:2023/3/26
 * Description:
 */
@Service
@Slf4j
public class FTServiceImpl  implements FTService {
    @Override
    public List<PageGoodsdDetailDTO> getGoodsDTOListByGoodsId(String goodsId, String salePrice) {
        //获取商品总数据
        String result="";
        List<PageGoodsdDetailDTO> pageDTOList=null;
        try{
            result= HttpRequest.get("https://h5.ftart.vip/api/mall/productSn/pageConsignment" +
                            "?productId="+goodsId+"&sortField=2&sortDesc=YES&pageNo=1&pageSize=100")
                    .execute().body();
        }catch (Exception e){
            log.error("##请求商品数据异常##："+e.getMessage());
        }
        if(!StringUtils.isEmpty(result)){
            FtGoodsDTO goodsDTO= JSONUtil.toBean(result,FtGoodsDTO.class);
            pageDTOList=new ArrayList<>();
            if(goodsDTO.getCode()==0){
                if(goodsDTO.getResult()!=null && !CollectionUtils.isEmpty(goodsDTO.getResult().getList())){
                    List<PageGoodsdDetailDTO> list=goodsDTO.getResult().getList();
                    for(PageGoodsdDetailDTO detailDTO:list){
                        if(detailDTO.getStateId()==1 && detailDTO.getConsignmentPrice().compareTo(new BigDecimal(salePrice))<=0){
                            pageDTOList.add(detailDTO);
                            //异步订单数据
                        }
                    }
                }
            }
        }
        return pageDTOList;
    }
    @Override
    public void createMarketOrder(String token, String goodsId,PageGoodsdDetailDTO detailDTO) {
        try{
            String url="https://h5.ftart.vip/api/mall/sk/co?pId="+goodsId+"&sId" +
                    "="+detailDTO.getId()+"&cuId="+detailDTO.getCuId();
            String response=
                    HttpRequest.put(url).header("authorization",
                            token).execute().body();
            FtGoodsDTO responseDTO= JSONUtil.toBean(response,FtGoodsDTO.class);
            log.info("#####创建订单结果####"+response);
            if(responseDTO.getCode()==0){
                DingDingUtils.sendMsg("热浪您有一个新的订单，请赶紧支付");
                log.error("#####创建订单成功####"+response);
            }
        }catch (Exception e){
            log.error("###创建订单失败###:"+e.getMessage()+",goodsId:"+goodsId+":"+detailDTO.toString());
        }
    }
    @Override
    public String getZFUrl(String id) {
        return null;
    }
}
